package top.eggcode.modules.user.control;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import top.eggcode.common.lang.BeanUnit;
import top.eggcode.common.mvc.Result;
import top.eggcode.common.mvc.ResultWrapper;
import top.eggcode.modules.user.model.AccountEntity;
import top.eggcode.modules.user.model.AccountRegisterForm;
import top.eggcode.modules.user.model.AccountView;
import top.eggcode.modules.user.service.AccountService;
import top.eggcode.modules.user.service.AuthService;
import top.eggcode.modules.user.service.UserService;
import top.eggcode.modules.user.tool.KeyTool;
import top.eggcode.base.SessionBaseController;
import top.eggcode.security.anno.InnerAuth;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;

/**
 * Title:
 * Description: TODO
 * Date: 2021/3/29 23:04
 *
 * @author JiaQi Ding
 * @version 1.0
 */
@Api(tags = "登录帐户")
@RequestMapping("/api")
@RestController
public class AccountController extends SessionBaseController {

    @Resource
    private AccountService selfService;

    @Resource
    private UserService userService;

    @Resource
    private AuthService authService;

    /**
     * 获取实体
     *
     * @param id 唯一标识
     * @return 实体
     */
    @RequiresPermissions("account:get")
    @ApiOperation("获取一个，根据id")
    @GetMapping("/account/{id}")
    public Result<?> getEntity(@PathVariable(name = "id") String id) {
        Optional<AccountEntity> entity = selfService.get(id);
        return ResultWrapper.successful(
                BeanUnit.transfer(entity.orElse(null), AccountView.class)
        );
    }

    /**
     * 获取全部实体
     *
     * @return 实体集合
     */
    @ApiOperation("获取全部")
    @GetMapping("/account")
    public Result<?> getAll() {
        List<AccountEntity> accounts = selfService.find();
        List<AccountView> views = new LinkedList<>();
        accounts.forEach(object -> views.add(BeanUnit.transfer(object, AccountView.class)));
        return ResultWrapper.successful(views);
    }

    /**
     * 注册
     */
    @ApiOperation("注册")
    @PostMapping("/account/register")
    public Result<AccountView> register(@RequestBody AccountRegisterForm view) {

        AccountEntity account = new AccountEntity().init();
        String ciphertext = KeyTool.encryptUserPassword(view.getPassword(), account.getSalt());

        account.setUsername(view.getUsername());
        account.setPassword(ciphertext);

        selfService.register(account);

        return ResultWrapper.successful(
                BeanUnit.transfer(account, AccountView.class)
        );
    }

    /**
     * 删除实体
     *
     * @param id 唯一标识
     * @return 消息
     */
    @ApiOperation("删除")
    @DeleteMapping("/account/delete")
    public Result<?> delete(String id) {
        Optional<AccountEntity> entity = selfService.get(id);
        if (entity.isPresent()) {
            selfService.delete(entity.get());
            return ResultWrapper.successful();
        } else {
            return ResultWrapper.failure("未找到对应数据");
        }
    }

    @InnerAuth
    @PostMapping("/account/findByName")
    public Result<?> getRoleCodeList(@RequestParam("username") String username) {
        return super.pack(selfService.findByUsername(username).get());
    }

}
